﻿using Notepad.Helper;
using Notepad.Model;
using System;
using System.Collections.Generic; 
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Notepad
{
    /// <summary>
    /// Notepad.xaml 的交互逻辑
    /// </summary>
    public partial class Note : Window
    {
        private double x, y;
        public Note(Nodes nodeM)
        {
            InitializeComponent();
            AllowsTransparency = true;
            GridTitle.MouseDown += GridTitle_MouseDown;
            var bus = (Brush)new BrushConverter().ConvertFromString(nodeM.BackColor);
            this.Background = bus;
            GridTitle.Background = bus;
            this.Top = nodeM.PosY;
            this.Left = nodeM.PosX;
            rtb.AppendText(nodeM.Content);
            x = Left;
            y = Top;
            SetFontColor();
        }


        private void AddBtn_Click(object sender, RoutedEventArgs e)
        {
            var filename = DateTime.Now.ToString("yyyyMMddHHmmssfff");
            new XmlHelper().CreateXml(filename);
            Note newNote = new Note(new Nodes());
            newNote.Tag = filename;
            newNote.Show();
        }

        private void MinusBtn_Click(object sender, RoutedEventArgs e)
        {

            if(MessageBox.Show("删了可就没了，删吗？", "操作提示", MessageBoxButton.OKCancel, MessageBoxImage.Asterisk) == MessageBoxResult.OK)
            {
                var fileName = this.Tag.ToString();
                System.IO.File.Delete(System.IO.Path.Combine(CommonHelper.SavePath, fileName + ".xml"));
                this.Close();

                //System.IO.DirectoryInfo folder = new System.IO.DirectoryInfo(CommonHelper.SavePath);
                //if (folder.GetFiles().Length <= 0)
                //{
                //    var newFileName = DateTime.Now.ToString("yyyyMMddHHmmssfff");
                //    new XmlHelper().CreateXml(newFileName);
                //    Note newNote = new Note(new Nodes());
                //    newNote.Tag = newFileName;
                //    newNote.Show();
                //}
            } 
        }

        private void BackColorBtn_Click(object sender, RoutedEventArgs e)
        {
            using (var colorDiag = new System.Windows.Forms.ColorDialog())
            {
                colorDiag.AllowFullOpen = true;
                colorDiag.FullOpen = true;
                colorDiag.AnyColor = true;
                if (colorDiag.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    var solidBrush = new System.Drawing.SolidBrush(colorDiag.Color);
                    var colorBrush = new SolidColorBrush(System.Windows.Media.Color.FromArgb(solidBrush.Color.A, solidBrush.Color.R, solidBrush.Color.G, solidBrush.Color.B));
                    var str = colorBrush.ToString();
                    var bus = (Brush)new BrushConverter().ConvertFromString(str);
                    this.Background = bus;
                    GridTitle.Background = bus;
                    new XmlHelper().SetNodeValue(Tag.ToString(), "BackColor", str);
                    SetFontColor();
                }
            }
        }

        private void GridTitle_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (this.Topmost == true) return;
            this.DragMove();
            if (x != Left && y != Top)
            {
                new XmlHelper().SetNodeValue(Tag.ToString(), "Position", $"{Left},{Top}");
                x = Left;
                y = Top;
            }

        }

        private void SaveBtn_Click(object sender, RoutedEventArgs e)
        {
            var txt = new TextRange(rtb.Document.ContentStart, rtb.Document.ContentEnd).Text;
            new XmlHelper().SetNodeValue(Tag.ToString(), "Content", txt);
        }

        private void DingBtn_Click(object sender, RoutedEventArgs e)
        {
            UnDingBtn.Visibility = Visibility.Visible;
            DingBtn.Visibility = Visibility.Collapsed;
            this.Topmost = false;
        }

        private void UnDingBtn_Click(object sender, RoutedEventArgs e)
        {
            UnDingBtn.Visibility = Visibility.Collapsed;
            DingBtn.Visibility = Visibility.Visible;
            this.Topmost = true;
        }

        private void SetFontColor()
        {
            Color color = (Color)ColorConverter.ConvertFromString(this.Background.ToString());
            if ((0.299 * color.R + 0.587 * color.G + 0.114 * color.B) / 255 > 0.5) //浅色
            {
                var bus = (Brush)new BrushConverter().ConvertFromString("#3d98d1");
                BackColorBtn.Foreground = bus;
                AddBtn.Foreground = bus;
                MinusBtn.Foreground = bus;
                rtb.Foreground = bus;
                UnDingBtn.Foreground = bus;
                DingBtn.Foreground = bus;
                SaveBtn.Foreground = bus;
            }
            else
            {
                var bus = (Brush)new BrushConverter().ConvertFromString("#FFFFFF");
                BackColorBtn.Foreground = bus;
                AddBtn.Foreground = bus;
                MinusBtn.Foreground = bus;
                rtb.Foreground = bus;
                UnDingBtn.Foreground = bus;
                DingBtn.Foreground = bus;
                SaveBtn.Foreground = bus;
            }
        }

    }
}
